package com.bdqn.t330.ch04.controller;


import com.bdqn.t330.ch04.entity.User;
import com.bdqn.t330.ch04.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

//@RestController
@Controller
public class UserController {

    @Resource
    UserService userService;

    @ResponseBody
    @RequestMapping("/list")
    public Object list(Integer id) {
        List<User> list = userService.getList(id);
        return list;
    }

    @ResponseBody
    @RequestMapping("/get")
    public String get(Integer id, Model model) {
        List<User> list = userService.getList(id);
        model.addAttribute("list", list);
        //return "index";
        return "redirect:www.baidu.com";
    }

    @RequestMapping("/login")
    public String login(String uname, String upwd, Model model, HttpSession session) {
        if (uname.equals("admin") && upwd.equals("123")) {
            session.setAttribute("user", uname);
            return "redirect:/list";
        } else {
            return "redirect:/login.jsp";
        }
    }

}
